package controllers;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;

import javax.ejb.EJB;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import beans.UserBeanRemote;
import dto.DTOUser;

public class UserDtoListController {
/**
 * Ienei Robert
 * returneaza lista tuturor userilor pentru admin
 */
	@EJB
	UserBeanRemote ubr;
	
	private String validateUser;
	private List<DTOUser> userList;	
	private List<DTOUser> validatedUserList;	
	
	public List<DTOUser> getValidatedUserList() {
		this.validatedUserList = ubr.getAllUsersDTO();
		return validatedUserList;
	}

	public void setValidatedUserList(List<DTOUser> validatedUserList) {
		this.validatedUserList = validatedUserList;
	}

	private Map<Integer, Boolean> checked = new HashMap<Integer, Boolean>();
	private String searchText;
	

	public UserDtoListController() {
		super();
	}
		
	public Map<Integer, Boolean> getChecked() {
		return checked;
	}

	public void setChecked(Map<Integer, Boolean> checked) {
		this.checked = checked;
	}


	public List<DTOUser> getUserList() {
		this.userList = ubr.getAllUsersDTOStartingWith(searchText);
		return userList;
	}

	public void setUserList(List<DTOUser> userList) {
		this.userList = userList;
	}
	

	/**
	 * @return the searchText
	 */
	public String getSearchText() {
		return searchText;
	}

	/**
	 * @param searchText the searchText to set
	 */
	public void setSearchText(String searchText) {
		this.searchText = searchText;
	}


	public String invalidate(){
		// /// internationalizare
		HttpSession session = (HttpSession) FacesContext.getCurrentInstance()
				.getExternalContext().getSession(true);
		LocaleController language = (LocaleController) session
				.getAttribute("language");
		String messageBundleName = "errorMessages.errors";
		if (language.getLanguage() == "ro") {
			messageBundleName += "_ro";
		} else if (language.getLanguage() == "hu") {
			messageBundleName += "_hu";
		} else if (language.getLanguage() == "de") {
			messageBundleName += "_de";
		}
		ResourceBundle messageBundle = ResourceBundle
				.getBundle(messageBundleName);
		// ///
		try{
			for (DTOUser item : userList) {
	            if (item.getSelected()) {
	                ubr.invalidateUser(item.getId());
	            }
	            else ubr.validateUser(item.getId());
	        }
			validateUser = messageBundle.getString("usersValidationSucces");
		}catch(Exception e){
			validateUser = messageBundle.getString("usersValidationSucces");
		}
		return "ok";
	}
	
	public String popUpOk() {
		validateUser = null;
		return null;
	}

	public String getValidateUser() {
		return validateUser;
	}

	public void setValidateUser(String validateUser) {
		this.validateUser = validateUser;
	}

}
